package com.xiecong.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.xiecong.service.StudentService;

import java.util.ArrayList;
import java.util.List;

public class StudentReaderListener implements ReadListener<StudentModel> {

    private StudentService studentService;
    private Integer clazzId;
    private List<StudentModel> students = new ArrayList<>();

    public StudentReaderListener(StudentService studentService, Integer clazzId){
        this.studentService = studentService;
        this.clazzId = clazzId;
    }

    @Override
    public void invoke(StudentModel studentModel, AnalysisContext analysisContext) {
        // 每读取到一行数据，调用业务逻辑处理
        students.add(studentModel);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 全部读取完后再处理
        studentService.importStudent(students, clazzId);
    }
}
